package com.talent.service.base.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName：XTokenRequestInterceptor
 * @Author: hszhou
 * @Date: 2025/2/28 11:10
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Component
@Slf4j
public class XTokenRequestInterceptor implements RequestInterceptor {


    /**
     * 请求拦截器
     * @param requestTemplate 请求模板
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {

        log.info("拦截器使用");
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(requestAttributes==null){
            log.info("没有请求上下文无法进行token的传递");
            return;
        }

        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String header = request.getHeader(HttpHeaders.AUTHORIZATION);

        if(StringUtils.isNotEmpty(header)){
            requestTemplate.header(HttpHeaders.AUTHORIZATION,header);
        }

    }
}
